import 'package:bruno/bruno.dart';
import 'package:common/common.dart';
import 'package:common/image/image_tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:miaowu/app/common/window_pop/public_btn_tem.dart';
import 'package:miaowu/app/common/window_pop/public_btn_widget.dart';

class ChatSettingsPage extends StatefulWidget {
  const ChatSettingsPage({super.key});

  @override
  State<ChatSettingsPage> createState() => _ChatSettingsPagePageState();
}

class _ChatSettingsPagePageState extends State<ChatSettingsPage> {
  List<PublicBtnItem>? list = [];

  PublicBtnItem? setpricechat;
  PublicBtnItem? chataudioreceive;
  PublicBtnItem? setpriceaudio;
  PublicBtnItem? chatvideoreceive;
  PublicBtnItem? setpricevideo;

  @override
  void initState() {
    fetchgChatPriceConfig();
    super.initState();
  }

  void fetchgChatPriceConfig() {
    HttpProvider.get('/user/getChatPriceSet', explain: '获取聊天价格设置初始化数据', success: (result) {
      if (result.data["data"] is List) {
        list = result.data["data"] == null
            ? []
            : (result.data["data"] as List).map((e) => PublicBtnItem.fromJson(e)).toList();
      }
      for (PublicBtnItem element in list ?? []) {
        if (element.key == 'setpricechat') {
          setpricechat = element;
        }
        if (element.key == 'chataudioreceive') {
          chataudioreceive = element;
        }
        if (element.key == 'setpriceaudio') {
          setpriceaudio = element;
        }
        if (element.key == 'chatvideoreceive') {
          chatvideoreceive = element;
        }
        if (element.key == 'setpricevideo') {
          setpricevideo = element;
        }
      }
      setState(() {});
    });
  }

  void fetchSubmitBaseSet({required String key, required String value}) {
    HttpProvider.post(
      '/user/subBaseSet',
      params: {'setKey': key, 'setVal': value},
      explain: '修改设置',
      success: (result) {
        fetchgChatPriceConfig();
      },
      failure: (result) {
        Toast.showText(result.message);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: BrnAppBar(title: '聊天设置', backgroundColor: Colors.white),
        backgroundColor: const Color(0xFFF6F6F6),
        body: Column(
          children: [
            if (setpricechat != null && setpricechat!.view == 1)
              _buildItem(
                item: setpricechat!,
                isShowLine: true,
              ),
            if (chataudioreceive != null && chataudioreceive!.view == 1)
              _buildItem(
                item: chataudioreceive!,
                isShowLine: true,
              ),
            if (setpriceaudio != null && setpriceaudio!.view == 1)
              _buildItem(
                item: setpriceaudio!,
              ),
            if (chatvideoreceive != null && chatvideoreceive!.view == 1)
              _buildItem(
                item: chatvideoreceive!,
              ),
            if (setpricevideo != null && setpricevideo!.view == 1)
              _buildItem(
                item: setpricevideo!,
              ),
          ],
        ));
  }

  Widget _buildItem({
    required PublicBtnItem item,
    bool isShowLine = false,
  }) {
    BtnClickParams? params = item.click?.params;
    String value = params?.value ?? '';

    Display? display = item.display;
    String subtitle = display?.subtitle ?? '';

    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        publicCheckClickAction(
          btnItem: item,
          alertCallBack: (item) {},
          clickAction: (btnItem) {
            List<BtnClickParamsOptionsItem>? options = item.click?.params?.options ?? [];
            List<String> dataArr = options.map((e) => e.txt ?? '').toList();
            BottomModalSheet.showText(Get.context!, dataArr: dataArr, title: item.title,
                clickCallback: (index, str) async {
              BtnClickParamsOptionsItem optionsItem = options[index];
              fetchSubmitBaseSet(key: btnItem.click!.option!, value: optionsItem.val!);
            });

            // if (item.key == 'setpricevideo') {
            //   List<BtnClickParamsOptionsItem>? options = item.click?.params?.options ?? [];
            //   List<String> dataArr = options.map((e) => e.txt ?? '').toList();
            //   BottomModalSheet.showText(Get.context!, dataArr: dataArr, title: '选择图片',
            //       clickCallback: (index, str) async {
            //     BtnClickParamsOptionsItem optionsItem = options[index];
            //     fetchSubmitBaseSet(key: 'setpricevideo', value: optionsItem.val!);
            //   });
            // }
          },
        );
      },
      child: Container(
        color: 1 == 1 ? Colors.white : Colors.transparent,
        padding: EdgeInsets.symmetric(horizontal: 15.w),
        child: 1 == 1
            ? Column(
                children: [
                  Container(
                    padding: const EdgeInsets.symmetric(vertical: 15),
                    child: Row(
                      children: [
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                children: [
                                  Text('${item.title}  ',
                                      style:
                                          TextStyle(fontSize: 15.sp, color: Colors.black, fontWeight: FontWeight.w500)),
                                  Image.asset('assets/app/images/mine/vip_icon.png', width: 44),
                                ],
                              ),
                              if (subtitle.isNotEmpty) const SizedBox(height: 5),
                              if (subtitle.isNotEmpty)
                                Text(subtitle,
                                    maxLines: 2,
                                    style: TextStyle(
                                        fontSize: 12.sp, color: const Color(0xFF999999), fontWeight: FontWeight.w500)),
                            ],
                          ),
                        ),
                        Text(value,
                            style: TextStyle(
                                fontSize: 15.sp, color: const Color(0xFF25D4D0), fontWeight: FontWeight.w500)),
                        const Icon(Icons.navigate_next_sharp, size: 25, color: Color(0xFF000000)),
                      ],
                    ),
                  ),
                  if (isShowLine) const Divider(color: Color(0xFFEEEEEE), height: 0.5)
                ],
              )
            : const SizedBox(height: 10),
      ),
    );
  }
}
